//
//  KKNetworkTool.swift
//  KKWeibo
//
//  Created by kassia on 16/7/11.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import AFNetworking

//定义请求的枚举进行判断
enum RequsetType: Int {
    case GET
    case POST

}
//网络工具类
class KKNetworkTool: AFHTTPSessionManager {

    //单例
    static let sharedTools: KKNetworkTool = {
    
        let tools = KKNetworkTool()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    //封装get 和 post
    func request(type: RequsetType, URLString: String,parameters: AnyObject?,callback: (response: AnyObject?,error: NSError?)->()){
    
        if type == .GET {
        
        GET(URLString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
            
            callback(response: response, error: nil)
            
            }, failure: { (_, error) -> Void in
                print(error)
                callback(response: nil, error: error)
                
        })
            
        } else {
            
            POST(URLString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    
                    callback(response: nil, error: error)
                    
            })
        
        }
    
    }

}
// 发送文字微博
extension KKNetworkTool {

    func update(access_token: String,status: String,callback: (response: AnyObject?, error: NSError?)->())->(){
    
        //  准备接口地址
        let url = "https://api.weibo.com/2/statuses/update.json"
        //  准备参数
        let params = [
            "access_token": access_token,
            "status": status
        ]
        //  使用共有函数进行网络请求数据
        request(.POST, URLString: url, parameters: params, callback: callback)
    
    
    }

}

// MARK: - 获取首页相关的数据
extension KKNetworkTool {

    //
    func getUserAccount(maxId: Int64,since_id: Int64,callback: (response: AnyObject?, error: NSError?)->()){
    
        // 准备url 
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
    
        // 准备参数
        let param = [
        
            "access_token": KKUserAccountModel.sharedUserAccount.accessToken!,
            "max_id": "\(maxId)",
            "since+id": "\(since_id)"
        ]
        
      //  print(url + "?access_token=" + KKUserAccountModel.sharedUserAccount.accessToken!)
        
        request(.GET, URLString: url, parameters: param, callback: callback)
    
    }

}

extension KKNetworkTool {

    //获取accesstoken
    func requsetAccesstoken(code: String,callback: (response: AnyObject?, error: NSError?)->() ){
    
        //准备Url
        let url = "https://api.weibo.com/oauth2/access_token"
        
        //参数
        let param = [
            "client_id": weiboAppKey,
            "client_secret": weiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": weiboRedirect_Uri
        ]
        
        //调用网络请求
        request(.POST, URLString: url, parameters: param, callback: callback)
    }
    
    
    //获取用户信息
    func requestUserAccount(userCount: KKUserAccount, callback: (response: AnyObject?, error: NSError?)->()){
    
        let url = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            "access_token": userCount.access_token!,
            "uid": "\(userCount.uid)"

        ]
        request(.GET, URLString: url, parameters: param, callback:callback)
    }

}
